import {unioformDateTransform} from '../../utils/index.js'
import {handleSql} from '../../database/mysql/index.js'

class taskListModel {
    constructor() {
        this.tableName = 'task_list';
    }

    // 获取用户的今日任务
    async getTodayTask(userInfo) {
        const {userId,roleCode,date,isUrgency} = userInfo;
        const sql = `SELECT id,taskName,startTime,endTime,isAlarmClock,isUrgency  FROM ${this.tableName}
                     WHERE userId = ? AND roleCode = ? AND isUrgency = ? AND createDate LIKE '${date}%' `

        let result = await handleSql(sql,[userId,roleCode,isUrgency])
        return result
    }

    // 新增任务  ——单个新增,只新增当日
    async addTask({userInfo,taskData}) {
        const {userId, roleCode} = userInfo;
        const {taskName, startTime,endTime,isAlarmClock, isUrgency} = taskData;
        const createDate = unioformDateTransform(new Date())
        const sql = `INSERT INTO ${this.tableName}(userId,roleCode,taskName,startTime,endTime,isUrgency,isAlarmClock,createDate) 
                     VALUES (?,?,?,?,?,?,?,?) `
        let result = await handleSql(sql,[userId,roleCode,taskName,startTime,endTime,isUrgency,isAlarmClock,createDate])
        if(result.affectedRows == 1){
            return true
        }else{
            return false
        }
    }
}   

export default new taskListModel();